package com.wyj.chat.websocket_chat.controller;

import com.wyj.chat.websocket_chat.pojo.Friends;
import com.wyj.chat.websocket_chat.pojo.User;
import com.wyj.chat.websocket_chat.service.FriendsService;
import com.wyj.chat.websocket_chat.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class FriendsController {

    @Autowired
    private FriendsService friendsService;

    @Autowired
    private UserService userService;

    @RequestMapping(value = "/friends/addFriend", method = RequestMethod.POST)
    @ResponseBody
    public boolean addFriend(Friends friends) {
        if (StringUtils.isEmpty(friends.getUserId())) {
            return false;
        }
        // 通过好友名查询用户
        User friUser = userService.getByName(friends.getName());
        Friends alsoFriend = new Friends();
        User user = userService.getById(friends.getUserId());
        alsoFriend.setName(user.getName());
        alsoFriend.setUserId(friUser.getId());
        boolean flag = friendsService.getListByUserIdAndFriName(friends.getUserId(), friends.getName());
        if (flag) {
            return false;
        }
        int i = friendsService.insertFriends(friends);
        friendsService.insertFriends(alsoFriend);
        if (i == 0) {
            return false;
        }
        return true;
    }


    @RequestMapping(value = "/friends/addFriends", method = RequestMethod.POST)
    @ResponseBody
    public boolean addFriends(Friends friends) {
        if (StringUtils.isEmpty(friends.getUserId()) || StringUtils.isEmpty(friends.getId()) || StringUtils.isEmpty(friends.getIconPath() )
                || StringUtils.isEmpty(friends.getSign())) {
            return false;
        }
        int i = friendsService.insertFriends(friends);
        if (i == 0) {
            return false;
        } else {
            return true;
        }
    }

    @RequestMapping(value = "/friends/deleteFriend", method = RequestMethod.POST)
    @ResponseBody
    public boolean deleteFriend(String id, String userId) {
        if (StringUtils.isEmpty(id) || StringUtils.isEmpty(userId)) {
            return false;
        }
        int i = friendsService.deleteFriends(id, userId);
        if (i == 0) {
            return false;
        } else {
            return true;
        }
    }

}
